package com.hnust.root.service.forword.wx;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hnust.root.dao.entity.domain.forward.Topic;
import com.hnust.root.dao.entity.domain.forward.UserLike;
import com.hnust.root.dao.entity.domain.forward.UserMessage;
import com.hnust.root.dao.mapper.wx.TopicMapper;
import com.hnust.root.dao.mapper.wx.UserLikeMapper;
import com.hnust.root.dao.mapper.wx.UserMassageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class UserLikeService extends ServiceImpl<UserLikeMapper, UserLike> {
    @Autowired
    private UserLikeMapper userLikeMapper;

    @Autowired
    private TopicMapper topicMapper;

    @Autowired
    private MessageService messageService;

    @Autowired
    private UserMassageMapper userMassageMapper;

    //点赞或者取消赞
    public boolean userLike(UserLike like) {
        UserLike userLike = getLikeByOpenIdTopicId(like.getOpenId(), like.getTopicId());
        if (userLike != null) {
            like.setId(userLike.getId());
        }
        if (like.isLiked()) {
            //增加点赞数
            topicMapper.addUserLikeCount(like.getTopicId());
            //添加用户消息
            addUserMessage(like);
        } else {
            //减少点赞数
            topicMapper.decreaseUserLikeCount(like.getTopicId());
        }
        return saveOrUpdate(like);
    }

    //获取用户是否点赞某一话题
    public boolean userHasLiked(String openId, int topicId) {
        UserLike userLike = getLikeByOpenIdTopicId(openId, topicId);
        boolean result = false;
        if (userLike != null) {
            result = userLike.isLiked();
        }
        return result;
    }

    //通过OpenId和topicId获取用户关注
    private UserLike getLikeByOpenIdTopicId(String openId, int topicId) {
        return userLikeMapper.selectOne(new QueryWrapper<UserLike>()
                .eq("topic_id", topicId)
                .eq("open_id", openId));
    }

    private void addUserMessage(UserLike like) {
        UserMessage userMessage = userMassageMapper.selectOne(new QueryWrapper<UserMessage>()
                .eq("topic_id",like.getTopicId())
                .eq("from_open_id",like.getOpenId())
                .eq("content_type","liked"));
        if (userMessage == null) {
            //添加点赞消息
          userMessage = new UserMessage();
        }
        Topic topic = topicMapper.selectById(like.getTopicId());
        userMessage.setTopicId(like.getTopicId());
        userMessage.setFromOpenId(like.getOpenId());
        userMessage.setToOpenId(topic.getOpenId());
        userMessage.setFlag(false);
        userMessage.setUpdatedAt(new Date());
        userMessage.setContentType("liked");
        messageService.addUserMessage(userMessage);
    }

    //通过topicId删除用户点赞数据
    public void deleteUserLikeByTopicId(int topicId) {
        userLikeMapper.delete(new QueryWrapper<UserLike>().eq("topic_id", topicId));
    }
}
